import datetime
import re

from itemloaders.processors import TakeFirst, MapCompose
from scrapy.loader import ItemLoader


def filter_numeric(text):
    '''
    如果内容为空，用0替换
    :param text:
    :return:
    '''

    # print('0text:', text)
    if text is None or text == '':
        text = 0
    else:
        ret = re.search(r'(\d+)', text, re.M)
        # print(ret)
        if ret is None or text == '':
            text = 0
        else:
            text = int(ret.group(1))
    # print('1text:', text)
    return text


# def filter_date(data):
#     '''
#     过滤得到日期字符串
#     :param data: 
#     :return: 
#     '''
#     return re.search('于 (.*) 发布', data).group(1)
#     


def date_convert(value):
    '''
    定义一个时间处理转换函数
    将 '\r\n\r\n            2018-03-06 18:18:18·  ' 转换成 datetime.date(2018, 3, 14, 18, 18, 18)
    :param value: 
    :return: 
    '''
    # print(value, ': 111')
    # if value is not None and value != '':
    #     value = str.strip(value[0])
    try:
        publish_date = datetime.datetime.strptime(str.strip(value), "%Y-%m-%d %H:%M:%S")
        # print(publish_date, ': 222')
    except Exception as e:
        publish_date = datetime.datetime.now()
    # print(publish_date, ': 3333')
    return publish_date


class PblogLoader(ItemLoader):
    default_output_processor = TakeFirst()
    # title_out = TakeFirst()
    publish_in = MapCompose(date_convert)
    # publish_out = TakeFirst()
    approval_in = MapCompose(filter_numeric)
    unlike_in = MapCompose(filter_numeric)
    # stamp_out = MapCompose(ItemLoader.default_output_processor)
    comment_in = MapCompose(filter_numeric)
    collection_in = MapCompose(filter_numeric)
